﻿// -----------------------------------------------------------------------
// <copyright file="ExcelExport.cs" company="Computer Science User">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace WindowsFormsApplication1
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using AMOOFWinter;
    using AMOOFWinter.Data;
    using Microsoft.Office.Interop.Excel;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
        public class ExcelExport
        {
            private Microsoft.Office.Interop.Excel.Application app = null;
            private Microsoft.Office.Interop.Excel.Workbook workbook = null;
            private Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
            private Microsoft.Office.Interop.Excel.Range workSheet_range = null;

            public ExcelExport()
            {
                createDoc();
            }

            public void createDoc()
            {
                try
                {
                    app = new Microsoft.Office.Interop.Excel.Application();
                    app.Visible = true;
                    workbook = app.Workbooks.Add(1);
                    worksheet = (Worksheet)workbook.Sheets[1];
                }
                catch (Exception e)
                {
                    Console.Write("Error");
                }
            }
            public void createHeaders(int row, int col, string htext, string cell1,
                string cell2, int mergeColumns, string b, bool font, int size, string
                fcolor)
            {
                worksheet.Cells[row, col] = htext;
                workSheet_range = worksheet.get_Range(cell1, cell2);
                workSheet_range.Merge(mergeColumns);
                switch (b)
                {
                    case "YELLOW":
                        workSheet_range.Interior.Color = System.Drawing.Color.Yellow.ToArgb();
                        break;
                    case "GRAY":
                        workSheet_range.Interior.Color = System.Drawing.Color.Gray.ToArgb();
                        break;
                    case "GAINSBORO":
                        workSheet_range.Interior.Color =
                System.Drawing.Color.Gainsboro.ToArgb();
                        break;
                    case "Turquoise":
                        workSheet_range.Interior.Color =
                System.Drawing.Color.Turquoise.ToArgb();
                        break;
                    case "PeachPuff":
                        workSheet_range.Interior.Color =
                System.Drawing.Color.PeachPuff.ToArgb();
                        break;
                    default:
                        //  workSheet_range.Interior.Color = System.Drawing.Color..ToArgb();
                        break;
                }

                workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
                workSheet_range.Font.Bold = font;
                workSheet_range.ColumnWidth = size;
                if (fcolor.Equals(""))
                {
                    workSheet_range.Font.Color = System.Drawing.Color.White.ToArgb();
                }
                else
                {
                    workSheet_range.Font.Color = System.Drawing.Color.Black.ToArgb();
                }
            }


            public void addData(int row, int col, string data,
                string cell1, string cell2, string format)
            {
                worksheet.Cells[row, col] = data;
                workSheet_range = worksheet.get_Range(cell1, cell2);
                workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
                workSheet_range.NumberFormat = format;
            }

            public void addImage(string path, string cell1, string cell2)
            {
                Bitmap bmp = (Bitmap) Bitmap.FromFile(@"C:\Users\cs480f11_17\Desktop\testimg.jpg");
                Clipboard.SetDataObject(bmp, true);
                workSheet_range = worksheet.get_Range(cell1, cell2);
                worksheet.Paste(workSheet_range, bmp);
            }

            public void addImage(Bitmap bmp, string cell1, string cell2)
            {
                Clipboard.SetDataObject(bmp, true);
                workSheet_range = worksheet.get_Range(cell1, cell2);
                worksheet.Paste(workSheet_range, bmp);
            }
        }
}
        /*
         * This is the code for use elsewhere to call this class and put stuff into the spreadsheet.
         * Much of this should be loopable to take some of the tedium out of it. If Excel can be
         * invoked in the background much like the solver and the file saved instead of opened,
         * that would be ideal, maybe even a status bar to show progress as this is pretty slow.
         */

        /*private void button1_Click(object sender, EventArgs e)
        {
            //String stuff = dsor.dataset.ToString() + "\n" + "MLE" + "\n" + dsor.stats.ToString(); 
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "XLS File|*.xls";
            save.ShowDialog();

            if (save.FileName != String.Empty)
            {
                doc excell_app = new doc();
                excell_app.createHeaders(5, 2, "Total of Products", "B5", "D5", 2, "YELLOW", true, 10, "n");
                //creates subheaders
                excell_app.createHeaders(6, 2, "Sold Product", "B6", "B6", 0, "GRAY", true, 10, "");
                excell_app.createHeaders(6, 3, "", "C6", "C6", 0, "GRAY", true, 10, "");
                excell_app.createHeaders(6, 4, "Initial Total", "D6", "D6", 0, "GRAY", true, 10, "");
                //add Data to cells
                excell_app.addData(7, 2, "114287", "B7", "B7", "#,##0");
                excell_app.addData(7, 3, "", "C7", "C7", "");
                excell_app.addData(7, 4, "129121", "D7", "D7", "#,##0");
                //add percentage row
                excell_app.addData(8, 2, "", "B8", "B8", "");
                excell_app.addData(8, 3, "=B7/D7", "C8", "C8", "0.0%");
                excell_app.addData(8, 4, "", "D8", "D8", "");
                //add empty divider
                excell_app.createHeaders(9, 2, "", "B9", "D9", 2, "GAINSBORO", true, 10, "");
                excell_app.addImage(@"C:\Users\cs480f11_17\Desktop\testimg.jpg", "B10", "B10");
            }
        }*/